using SqlSugar;
using LuUpgrade.Model;

namespace LuUpgrade.Utils
{
    public class DbContext
    {
        public SqlSugarClient Db { get; }
        // private readonly string host;
        // private readonly string port;
        // private readonly string database;
        // private readonly string username;
        // private readonly string password;

        public DbContext()
        {
            // var configManager = new ConfigManager("E:\\DotNetCode\\lu-upgrade\\Config\\config.ini");
            // host = configManager.GetConfigValue("Host");
            // port = configManager.GetConfigValue("Port");
            // database = configManager.GetConfigValue("Database");
            // username = configManager.GetConfigValue("Username");
            // password = configManager.GetConfigValue("Password");
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Data Source=127.0.0.1;Initial Catalog=demo;User ID=sa;Password=123456",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });

            // 测试连接
            try
            {
                Db.Ado.CheckConnection();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            // 输出SQL语句
            // Db.Aop.OnLogExecuted = (sql, pars) =>
            // {
            //     Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            //     Console.WriteLine();
            // };
        }
    }
}